﻿var objectId = request('objectId');
var objectType = request('objectType');

var acceptClick;
var bootstrap = function ($, learun) {
    "use strict";

    var selectData = [];
    var treeData;
    var checkModuleIds = [];
    var itemType = 0;//  数据权限类型

    function setTreeData() {
        if (!!selectData) {
            $('#tree').lrtreeSet('setCheck', selectData);
        }
        else {
            setTimeout(setTreeData, 100);
        }
    }

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        /*绑定事件和初始化控件*/
        bind: function () {
            // 权限设置
            $('[name="itemType"]').on('click', function () {
                var $this = $(this);
                var value = $this.val();
                itemType = value;
                if (value != '9') {
                    $('#shcemeAuthorizeBg').show();
                }
                else {
                    $('#shcemeAuthorizeBg').hide();
                }
            });

            learun.httpAsyncGet(config.base_server() + 'orgs/tree', function (res) {
                if (res.resp_code == 200) {
                    treeData = res.data;
                    setTimeout(function () {
                        $('#tree').lrtree({
                            data: treeData
                        });
                        if (!!objectId) {
                            setTreeData();
                        }
                    }, 10);
                }
            });
        },
        /*初始化数据*/
        initData: function () {
            if (!!objectId) {
                $.lrSetForm(config.base_server() + 'permissions/findByObject?objectId=' + objectId + "&objectType=" + objectType, function (data) {
                    if (!!data) {
                        $('#form').lrSetFormData(data);
                        if (data.itemIds != null) {
                            for (var i = 0; i < data.itemIds.length; i++) {
                                selectData.push(data.itemIds[i]);
                            }
                        }
                    }
                });
            }
        },
    };

    acceptClick = function (callBack) {
        var list = [];
        var checkFormIds = $('#tree').lrtreeSet('getCheckNodeIds');
        $.each(checkFormIds, function (id, item) {
            if (item.indexOf('_LRDataItem') == -1) {
                list.push(item);
            }
        });

        if (itemType != '9') {
            list = [];
        }

        var postData = {
            objectId: objectId,
            objectType: objectType,
            itemType: itemType,
            itemIds: list
        };

        $.lrSaveForm(config.base_server() + 'permissions/saveOrUpdate?access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };

    page.init();
}